package org.example.dao;

import org.example.pojo.Follow;
import org.example.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FollowDao {

    // 添加 关注
    public boolean addFollow(int userId, int authorId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into follow(user_id,author_id,create_time,update_time) values(?,?,now(),now())";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, userId);
        ps.setInt(2, authorId);
        return ps.executeUpdate()>0;
    }

    // 查看 用户id 关注的列表的用户， 用户 关注的 人数
    public List<Follow> listFollow(int userId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from follow where user_id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, userId);
        ResultSet rs = ps.executeQuery();
        List<Follow> ls = new ArrayList<>();
        while (rs.next()) {
            Follow follow = new Follow();
            follow.setId(rs.getInt("id"));
            follow.setUserId(rs.getInt("user_id"));
            follow.setAuthorId(rs.getInt("author_id"));
            follow.setCreateTime(rs.getDate("create_time"));
            follow.setUpdateTime(rs.getDate("update_time"));
            ls.add(follow);
        }
        return ls;
    }

    // 分页 查看 关注的 用户，// 通过 id 去 获取 到 作者的id 然后 获取到作者的 信息
    public List<Follow> listFollowPage(int userId, int page, int pageSize) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from follow where user_id = ? limit ?,?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, userId);
        ps.setInt(2, page*pageSize);
        ps.setInt(3, pageSize);
        ResultSet rs = ps.executeQuery();
        List<Follow> ls = new ArrayList<>();
        while (rs.next()) {
            Follow follow = new Follow();
            follow.setId(rs.getInt("id"));
            follow.setUserId(rs.getInt("user_id"));
            follow.setAuthorId(rs.getInt("author_id"));
            follow.setCreateTime(rs.getDate("create_time"));
            follow.setUpdateTime(rs.getDate("update_time"));
//            System.out.println(follow);
            ls.add(follow);
        }
        return ls;
    }

    // 取消 关注
    public boolean deleteFollow(int userId, int authorId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "delete from follow where user_id = ? and author_id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, userId);
        ps.setInt(2, authorId);

        return ps.executeUpdate()>0;  //返回 true 表示删除成功
    }

    // 查看 用户 是否关注了 作者
    public boolean isFollow(int userId, int authorId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from follow where user_id = ? and author_id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, userId);
        ps.setInt(2, authorId);
        ResultSet rs = ps.executeQuery();
        return rs.next();
    }

    // 获取 更具用户id 被关注的数量
    public int getFollowCount(int userId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(*) from follow where author_id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, userId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    }

    // 用户 关注 作者的 人数
    public int getFollowerCount(int authorId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(*) from follow where user_id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, authorId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    }

}
